import {
  Controller,
  Get,
  Post,
  Body,
  HttpCode,
  Query,
  UseGuards,
  Headers,
  Request,
} from '@nestjs/common';
import {
  ApiResponse,
  ApiOperation,
  ApiBearerAuth,
  ApiTags,
  ApiBody,
  ApiParam,
} from '@nestjs/swagger';
import { throwHttp } from 'src/utlis';
import {
  CreateRoleDto,
  GetRolePageDto,
  RoleAuthConfigDto,
  UpdateRoleDto,
} from './dto/role.dto';
import { RoleFieldName, RoleResult } from './entities/role.entity';
import { RoleService } from './role.service';
import { JwtService } from '@nestjs/jwt';
import { TokenDto } from 'src/common/auth/constants';
import { isTrue } from 'src/utlis/tools';
import { AuthGuard } from '@nestjs/passport';
import { MethodType, OperateType, OperationRecordType } from '../operation-record/entities/operation-record.entity';
import { OperationRecordService } from '../operation-record/operation-record.service';
@ApiBearerAuth() // swagger文档设置token
@ApiTags('权限')
@Controller('admin/role')
export class RoleController {
  constructor(
    private readonly roleService: RoleService,
    private readonly operationRecordService: OperationRecordService,
    private readonly jwtService: JwtService
  ) { }
  /** 角色列表 */
  @HttpCode(200)
  @Get('/getRolePage')
  @ApiResponse({
    status: 200,
    description: '角色列表',
    type: RoleResult,
  })
  @ApiOperation({
    tags: ['获取角色列表'],
    summary: '获取角色列表',
    operationId: 'getRolePage',
  })
  @UseGuards(AuthGuard('jwt')) // 自定义守卫
  getRolePage(@Query() params: GetRolePageDto) {
    return this.roleService.getRolePage(params);
  }

  @HttpCode(200)
  @Post('/createRole')
  @ApiBody({
    description: '创建角色',
    type: CreateRoleDto,
  })
  @UseGuards(AuthGuard('jwt')) // 自定义守卫
  @ApiOperation({ summary: '创建角色', operationId: 'createRole' })
  async createRole(@Body() body: CreateRoleDto, @Request() req, @Headers('authorization') authorization) {
    if (!isTrue(authorization)) {
      throwHttp('获取token失败');
    }
    const token = authorization.split(' ');
    const user: TokenDto = this.jwtService.decode(token[1]) as TokenDto;
    const data = await this.roleService.createRole(body, user.userId);
    await this.operationRecordService.createOperationRecord(
      {
        operation: '创建成功',
        name: '创建角色',
        method: MethodType.POST,
        outside: OperationRecordType.role,
        operateType: OperateType.add,
        interfacePath: 'admin/role/createRole',
        ip: req.ip,
        dataId: data.data.id
      }, user.userId)
    return data.msg
  }
  @HttpCode(200)
  @Post('/updateRole')
  @ApiBody({
    description: '更新角色',
    type: UpdateRoleDto,
  })
  @UseGuards(AuthGuard('jwt')) // 自定义守卫
  @ApiOperation({ summary: '更新角色', operationId: 'updateRole' })
  async updateRole(@Body() body: UpdateRoleDto, @Request() req, @Headers('authorization') authorization) {
    if (!isTrue(authorization)) {
      throwHttp('获取token失败');
    }
    const token = authorization.split(' ');
    const user: TokenDto = this.jwtService.decode(token[1]) as TokenDto;
    const data = await this.roleService.updateRole(body);
    if (body.diff) {
      const diff = this.operationRecordService.convertToChinese(body.diff as any[], RoleFieldName)
      await this.operationRecordService.createOperationRecord(
        {
          operation: diff,
          name: '更新角色',
          method: MethodType.POST,
          outside: OperationRecordType.role,
          operateType: OperateType.edit,
          interfacePath: 'admin/role/updateRole',
          ip: req.ip,
          dataId: data.data.id
        }, user.userId)
    }

    return data.msg
  }
}
